package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.common.util.JwtUtils;
import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVo;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/api/core/userInfo")
//@CrossOrigin
public class ApiUserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/testAuth")
    public Result testAuth(HttpServletRequest request){
        System.out.println("鉴权测试");
        String userId = request.getHeader("userId");
        System.out.println("userId = " + userId);
        return Result.ok();
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){

        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //断言判断
        Assert.notNull(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        boolean b = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(b,ResponseEnum.MOBILE_ERROR);
        Assert.notNull(password, ResponseEnum.PASSWORD_NULL_ERROR);
        Assert.notNull(code, ResponseEnum.CODE_NULL_ERROR);

        userInfoService.register(registerVo);

        return Result.ok().message("注册成功");
    }

    @ApiOperation("用户登入")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO, HttpServletRequest request){
        //request.getRemoteAddr()获取ip
        UserInfoVO userInfoVO = userInfoService.login(loginVO,request.getRemoteAddr());

        return Result.ok().data("userInfo",userInfoVO);
    }

    @ApiOperation("手机号是否存在")
    @GetMapping("/checkMobile/{mobile}")
    public Boolean checkMobile(@PathVariable String mobile){

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>()
                .eq("mobile",mobile);

        int count = userInfoService.count(wrapper);

        return count > 0;
    }

    @ApiOperation("用户中心初始化")
    @GetMapping("/initial")
    public Result initial(HttpServletRequest request){

        String userId = request.getHeader("userId");

        UserInfo userInfo = userInfoService.initial(userId);

        return Result.ok().data("userInfo",userInfo);
    }

    @GetMapping("/editHead")
    public Result editHead(@RequestParam String url,HttpServletRequest request){

        String userId = request.getHeader("userId");
        userInfoService.editHead(url,userId);

        return Result.ok().message("上传头像成功");
    }

}

